"""
合并大文件 按行转移到一个文件中
"""
import os
import shutil
import sys

import click

cur_dir_fullpath = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.abspath(os.path.join(cur_dir_fullpath, '../../')))  # 插入
from xjlibrary.configread import MyConfigParser
from xjlibrary.our_file_dir import BaseDir


def start(filePath, output):
    if not os.path.exists(output):
        os.makedirs(output)
    outputFile = BaseDir.get_new_path(output, "all.big_html")
    for fLine in BaseDir.read_file_rb_mode_yield(filePath):
        BaseDir.add_file_ab_mode(fLine, outputFile)
    shutil.move(filePath, output)
    print("合并成功")


@click.command()
@click.option('--inputs', default='', type=str, help='请输入目录路径的列表')
@click.option('--output', default='', type=str, help='请输入输出路径')
@click.option('--config', default='', type=str, help='请输入配置文件路径')
def main(inputs, output, config):
    print("inputs is %s " % inputs)
    print("output is %s " % output)
    print("config is %s " % config)
    if inputs and output:
        inputs = eval(inputs)
    else:
        cf = MyConfigParser(config).get_conf_dict()
        print(cf)
        inputs = cf["args"]["inputs"]
        output = cf["args"]["output"]
        inputs = eval(inputs)

    for input in inputs:
        for filePath in BaseDir.get_dir_all_files(input):
            print("开始合并:" + filePath)
            start(filePath, output)


if __name__ == "__main__":
    main()
